package com.qing.spark.dao

import scala.collection.mutable.ListBuffer

/**
  * Created by wuliao on 2018/4/11.
  */
object MemoryDao {
  def save(lists: ListBuffer[Tuple4[java.lang.String, Double, java.lang.String, Int]]): Unit = {

    val con = MysqlUtils.getInstance.getCon()

    for (temp <- lists) {

      val url = "select * from ip_day_memory where ip_day='" + temp._1+"'"
      MysqlUtils.getInstance.reCon()
      val result = con.prepareStatement(url).executeQuery()
      if (con != null && result.next()) {
        //已经存在
        val counter_volume = (result.getDouble("counter_volume") + temp._2) / 2

        val psql = con.prepareStatement("update ip_day_memory set counter_volume = ? where ip_day = ?");
        psql.setDouble(1, counter_volume)
        psql.setString(2, temp._1)
        psql.executeUpdate()
      } else {
        //不存在
        val sql = "insert into ip_day_memory(ip_day,counter_volume,ip,day) values(?,?,?,?)"
        val ptmt = con.prepareStatement(sql)
        ptmt.setString(1, temp._1)
        ptmt.setDouble(2, temp._2)
        ptmt.setString(3, temp._3)
        ptmt.setInt(4, temp._4)
        ptmt.execute()
      }
    }

  }
}
